// JORGE FERNANDO GOMEZ
// 1259371
// 189631
// LEC A1
// MARTIN MÜLLER
// D02
// YANG LIU


#line 10 "draw2.c"

/* figStack module will handle a figure stack storing figure properties. */
#include "figStack.h"

/* read module reads input files into memory. */
#include "read.h"

/* process module handles commands for Sketchpad. */
#include "process.h"


/*
 * draw2.c takes an input file, performs some simple parsing operations
 * through it, and outputs commands to Sketchpad.jar or stdout depending
 * on each keyword found.
 *
 * draw2.c starts by confirming an input file was received.  If one is not
 * found, the program prints a message and exits.
 *
 * draw2.c now checks for ./Sketchpad.jar or ./sketchpad.jar.  It it can't
 * be found, the program prints a message and exits.
 *
 * Four variables declared:
 *
 *      Figures is a FigStack structure which will hold figure definitions.
 *      That is, figures' names, draw segments, starting (x, y) pen
 *      positions, as well as (x, y) deltas.
 *
 *      Lines is a struct Line pointer.  Each index will store a single
 *      line, as well as that line's starting keyword type.
 *
 *      inputFile and outputFile serve as additional I/O streams.
 *
 *      inputFileLength is defined in read.c.
 *      inputFileLength holds the line count of the input file.
 *
 *
 * The program prints a welcome message specifying the name of the
 * executable, the current date via system("date"); and the current input
 * file name that the program will attempt to evaluate.
 *
 * draw2.c opens the input file with fopen() and an output stream wih
 * with popen(); each of these are stored in inputFile and outputFile
 * respectively.  draw2 adds our streams to our stream list for management.
 * If one of these operations fail, the program will print
 * and error to stderr and terminate.
 *
 * Line count is computed and stored in inputFileLength.
 *
 * readFile() is called to read each line on the input file and store it on
 * Lines accordingly.  If readFile() encounters a problem, an error is
 * printed to stderr and program terminates.
 *
 * Program now classifies each line according to eight different keywords
 * and tags each one of them accordingly. *
 *
 * If classifyLines() encountered bad input, an error is printed to stderr
 * and the program terminates.
 *
 * Now the program has the information necessary to carry out any command
 * found on in Lines. executeCommands() is called, and depending on each tag
 * at found on each line: a figure is drawn in Sketchpad.jar or its properties
 * printed to stdout with drawPrintFigure(), a figure is translated by some
 * deltas (x, y) with translateFigure(), a figure is rotated by some specified
 * angle with rotateFigure(), or child commands are piped to Sketchpad with
 * sendChildCommands().
 *
 *
 * The program now closes frees used memory and closes its open streams and
 * returns 0 if successful. */



int main( int argc, const char * argv[] ) {

    /****************** Check environment *******************/
    if ( argc != 2 )
        kill (0);

    char exec_c[23+1];

    FILE * sketchpad;
    if ( (sketchpad = fopen("Sketchpad.jar", "r")) != NULL )
        strncpy(exec_c, "java -jar Sketchpad.jar", 24);

    else if ( (sketchpad = fopen("sketchpad.jar", "r")) != NULL )
        strncpy(exec_c, "java -jar sketchpad.jar", 24);

    else
        kill (__LINE__);

    fclose(sketchpad);


    int debug = 0;

    FILE * inputFile = fopen(argv[1], "r");

    if ( !inputFile )
        kill(0);

    addStream(&inputFile, 'f', 0);

    FILE * outputFile = popen(exec_c, "w");

    if ( !outputFile )
        kill(__LINE__);

    addStream(&outputFile, 'p', 1);
    /********************************************************/


    char * buff = (char *) malloc(MAXLEN+1);

    if ( buff == NULL )
        kill(__LINE__);

    memset(buff, '\0', (MAXLEN+1));

    /* Get input file line length. */
    for ( inputFileLength = 0;      /* Get file length (line count). */
          fgets(buff, MAXLEN+1, inputFile) != NULL;
          inputFileLength++ )
        ;

    free(buff);


    /* Holds a line read from a file. */
    Line * Lines = initLines(inputFileLength);
            //Line * Lines = calloc((size_t) inputFileLength, sizeof(Line));
            //Line Lines[inputFileLength];
            //Line * Lines = malloc(sizeof(Line)*inputFileLength);
            //memset(Lines, 0, sizeof(Line)*inputFileLength);

    /* Create a single figure stack that will
     * hold figure's properties. */
    FigStack Figures;
    initstack( &Figures, 20 );


    /******* MAIN PROGRAM *******/

    /* Welcome message. */
    printf("%s started on ", argv[0]);
    fflush(NULL);
    system("date");
    printf("Input file: ./%s\n", argv[1]);


    /* Read the input file and store each line read in our
     * Lines array, or terminate if nothing was read. */
    if ( (debug = readFile(Lines, inputFile)) )
        kill(__LINE__);

    /* Find out each of the lines' stored in Lines properties.
     * Terminate if we bad input was encountered. */
    if ( (debug = classifyLines(Lines)) )
        kill(__LINE__);

    /* Execute an action according to each of the
     * lines' in Lines keyword. */
    executeCommands( outputFile, Lines, &Figures);

    /* Free our initiated stacks memory. */
    destroyFigStack( &Figures );
    destroyStackList();

    /* Close our open streams. */
    closeStreams();
    free(Lines);


    return 0;
}
